/**
 * 
 */
package cn.terrence.myrpc.common.serializable.impl;

import java.io.InputStream;
import java.io.OutputStream;

import cn.terrence.myrpc.common.serializable.CoderContext;
import cn.terrence.myrpc.common.serializable.CoderException;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

/**
 * @author Terrence2013
 * 2018年1月19日 下午2:37:01
 * Hessian序列化实现
 */
public class HessianCoder extends AbsCoder {

	public HessianCoder(CoderContext context) {
		super(context);
	}

	@Override
	public void code(OutputStream outputStream, Object object)
			throws CoderException {
		try{
			 HessianOutput ho = new HessianOutput(outputStream);  
			 ho.writeObject(object); 
		}catch(Exception ex){
			throw new CoderException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T decode(InputStream inputStream, Class<T> clazz)
			throws CoderException {
		try{
			HessianInput hi = new HessianInput(inputStream);  
		    return (T)hi.readObject();
		}catch(Exception ex){
			throw new CoderException(ex);
		}
	}

}
